package com.uplift.common.result;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageInfo;
import com.uplift.common.result.impl.LayuiResult;

public class ResultWrapper4Layui {
	public static String SUCC = "1";
	public static String FAIL = "0";

	/**
	 * 包装带分页列表的数据成为Map结构
	 * 
	 * @param list
	 * @param status
	 * @param message
	 * @return Map<String, Object>
	 */
	public static IResultable wrapListPage(List<?> list, String status, String message) {
		LayuiResult layuiResult = new LayuiResult();

		if (list == null) {
			layuiResult.setCount(0);
			layuiResult.setData("");
		} else {
			@SuppressWarnings("unchecked")
			PageInfo<Object> pi = new PageInfo<Object>((List<Object>) list);
			layuiResult.setCount(pi.getTotal());
			layuiResult.setData(list);
		}

		layuiResult.setCode(status);
		layuiResult.setMsg(message);

		return layuiResult;
	}

	/**
	 * 包装带分页列表的数据成为Map结构
	 *
	 * @param list
	 * @param list
	 * @param status
	 * @param message
	 * @return Map<String, Object>
	 */
	public static IResultable wrapListPage(List<?> list, Map<?,?> subData, String status, String message) {
		LayuiResult layuiResult = new LayuiResult();

		if (list == null) {
			layuiResult.setCount(0);
			layuiResult.setData("");
		} else {
			@SuppressWarnings("unchecked")
			PageInfo<Object> pi = new PageInfo<Object>((List<Object>) list);
			layuiResult.setCount(pi.getTotal());
			layuiResult.setData(list);
		}

		if(subData != null) {
			layuiResult.setSubData(subData);
		}

		layuiResult.setCode(status);
		layuiResult.setMsg(message);

		return layuiResult;
	}

	/**
	 * 封装没有分页的list
	 * 
	 * @param list
	 * @param status
	 * @param message
	 * @return Map<String, Object>
	 */
	public static IResultable wrapListNoPage(List<?> list, String status, String message) {
		LayuiResult layuiResult = new LayuiResult();

		layuiResult.setCount(0);
		layuiResult.setData(list);
		layuiResult.setCode(status);
		layuiResult.setMsg(message);

		return layuiResult;
	}

	/**
	 * 包装普通Map响应，data参数可null
	 * 
	 * @param data
	 * @param status
	 * @param message
	 * @return Map<String, Object>
	 */
	public static IResultable wrapMap(Map<?, ?> data, String status, String message) {
		LayuiResult layuiResult = new LayuiResult();
		if (data == null || data.isEmpty()) {
			layuiResult.setData("");
		} else {
			layuiResult.setData(data);
		}
		layuiResult.setCode(status);
		layuiResult.setMsg(message);

		return layuiResult;
	}
}
